package mest.controller.Modeling;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import mest.VO.Modeling.WarehouseSaveFile_manytomany.savefileVO;
import mest.entity.warehouse_file;
import mest.entity.warehousesave_manytomany;
import mest.service.Modeling.IwarehouseFileService;
import mest.service.system.DBService;
import mest.util.mestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @auther Machenike
 * @create 2019-11-16 1:22
 */
@RestController
@CrossOrigin
@RequestMapping(value = "/warehouseFile")
public class warehouseFile {
    @Resource
    private IwarehouseFileService ipfs;

    @Autowired
    private DBService dbService;

    @RequestMapping(value = "/insert")
    public String insert(warehouse_file wt, HttpServletRequest request) {
        String theirDb = mestUtil.getCompanyDB(request);

        int i = ipfs.insertWarehouseFileType(wt, theirDb);
        if (i >= 1)
            return JSON.toJSONString("添加成功");
        else
            return JSON.toJSONString("添加失败");
    }

    @RequestMapping(value = "/baseSelect", produces = {"application/json;charset=utf-8", "text/plain;charset=utf-8"})
    public String baseSelect(int index, int pageSize, HttpServletRequest request) {
        String theirDb = mestUtil.getCompanyDB(request);

        List<warehouse_file> warehouseFilePage = ipfs.findWarehouseFilePage(index, pageSize, theirDb);
        int i = ipfs.selectCount(theirDb);
        Map map = new HashMap();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", i);
        map.put("data", warehouseFilePage);
        return JSON.toJSONString(map);
    }

    //    toolbar查看bom详情
    @RequestMapping(value = "/saveFileDetail", produces = {"application/json;charset=utf-8", "text/plain;charset=utf-8"})
    public String saveFileDetail(int wfId, String commName, int index, int pageSize, HttpServletRequest request) {
        String theirDb = mestUtil.getCompanyDB(request);
        System.out.println(wfId);
        List<savefileVO> savefileByWFId = ipfs.findSaveByWFId(wfId, commName, index, pageSize, theirDb);
        int i = ipfs.SaveCount(wfId,commName, theirDb);
        Map map = new HashMap();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", i);
        map.put("data", savefileByWFId);
        return JSON.toJSONString(map);
    }

    //    toolbar添加save
    @RequestMapping(value = "/addSave", produces = {"application/json;charset=utf-8", "text/plain;charset=utf-8"})
    public String addBom(warehousesave_manytomany wsmtm, HttpServletRequest request) {
        String theirDb = mestUtil.getCompanyDB(request);
        Integer userId = dbService.getUserId(); //获取操作人ID
        int i = ipfs.insertManytomany_WarehouseAndSave(wsmtm, theirDb,userId);
        if (i >= 1)
            return JSON.toJSONString("添加成功");
        else
            return JSON.toJSONString("添加失败");
    }

    // toolbar 修改save
    @RequestMapping(value = "/updateSave", produces = {"application/json;charset=utf-8", "text/plain;charset=utf-8"})
    public String updateBom(savefileVO wsmtm, HttpServletRequest request) {
        String theirDb = mestUtil.getCompanyDB(request);
        int i = ipfs.updateManytomany_WarehouseAndSave(wsmtm, theirDb);
        if (i >= 1)
            return JSON.toJSONString("修改成功");
        else
            return JSON.toJSONString("修改失败");
    }

    // toolbar 删除save
    @RequestMapping(value = "/delSave", produces = {"application/json;charset=utf-8", "text/plain;charset=utf-8"})
    public String delSave(savefileVO wsmtm, HttpServletRequest request) {
        String theirDb = mestUtil.getCompanyDB(request);
        int i = ipfs.delManytomany_WarehouseAndSave(wsmtm, theirDb);
        if (i >= 1)
            return JSON.toJSONString("删除成功");
        else
            return JSON.toJSONString("删除失败");
    }

    //根据bomid和存货id查询是否已在多对多表存在

    @RequestMapping("/examine")
    public String examine(@RequestBody JSONObject json, HttpServletRequest request){
        String theirDb = mestUtil.getCompanyDB(request);
        Map<String,Object> map = json.toJavaObject(Map.class);
        map.put("theirDb",theirDb);
        return ipfs.examine(map);
    }

}
